
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class Screen extends JPanel implements Runnable {

    public Thread thread = new Thread(this);

    public static boolean isFirst = true;

    public static Room room;

    public Element element;

    public static int rowsCleaned = 0;

    public static int level = 0;

    public static boolean life = true;

    public Screen(Frame frame) {

        frame.addKeyListener(new KeyListener() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();

                boolean touched = false;

                switch (keyCode) {
                    case KeyEvent.VK_LEFT:
                        for (int i = 0; i < 4; i++) {
                            if (element.elementBlocks[i][0] == 0
                                    || (element.elementBlocks[i][1] >= 0
                                    && room.block[element.elementBlocks[i][1] + 1][element.elementBlocks[i][0] - 1].id == 1)) {
                                touched = true;
                            }
                        }

                        if (!touched) {
                            for (int i = 0; i < 4; i++) {
                                element.elementBlocks[i][0]--;
                            }
                        }
                        break;
                    case KeyEvent.VK_RIGHT:
                        touched = false;
                        for (int i = 0; i < 4; i++) {
                            if (element.elementBlocks[i][0] == room.fieldWidth - 1
                                    || (element.elementBlocks[i][0] < room.fieldWidth - 1
                                    && room.block[element.elementBlocks[i][1]][element.elementBlocks[i][0] + 1].id == 1)) {
                                touched = true;
                            }
                        }

                        if (!touched) {
                            for (int i = 0; i < 4; i++) {
                                element.elementBlocks[i][0]++;
                            }
                        }
                        break;
                    case KeyEvent.VK_DOWN:
                        touched = false;
                        for (int i = 0; i < 4; i++) {
                            if (element.elementBlocks[i][1] == room.fieldHeight - 1
                                    || (element.elementBlocks[i][1] < room.fieldHeight - 1
                                    && room.block[element.elementBlocks[i][1] + 1][element.elementBlocks[i][0]].id == 1)) {
                                touched = true;
                            }
                        }

                        if (!touched) {
                            for (int i = 0; i < 4; i++) {
                                element.elementBlocks[i][1]++;
                            }
                        }
                        break;
                    case KeyEvent.VK_SPACE:
                        int pos = element.possition;

                        switch (element.type) {
                            case 2:
                                if (pos == 1) {
                                    element.elementBlocks[0][0]++;
                                    element.elementBlocks[0][1]--;
                                    element.elementBlocks[2][0]--;
                                    element.elementBlocks[2][1]++;
                                    element.elementBlocks[3][1] -= 2;
                                    element.possition++;
                                } else if (pos == 2) {
                                    element.elementBlocks[0][0]++;
                                    element.elementBlocks[0][1]++;
                                    element.elementBlocks[2][0]--;
                                    element.elementBlocks[2][1]--;
                                    element.elementBlocks[3][0] += 2;
                                    element.possition++;
                                } else if (pos == 3) {
                                    element.elementBlocks[0][0]--;
                                    element.elementBlocks[0][1]++;
                                    element.elementBlocks[2][0]++;
                                    element.elementBlocks[2][1]--;
                                    element.elementBlocks[3][1] += 2;
                                    element.possition++;
                                } else if (pos == 4) {
                                    element.elementBlocks[0][0]--;
                                    element.elementBlocks[0][1]--;
                                    element.elementBlocks[2][0]++;
                                    element.elementBlocks[2][1]++;
                                    element.elementBlocks[3][0] -= 2;
                                    element.possition = 1;
                                }
                                break;
                            case 3:
                                if (pos == 1) {
                                    element.elementBlocks[0][0] += 2;
                                    element.elementBlocks[1][0]++;
                                    element.elementBlocks[1][1]++;
                                    element.elementBlocks[3][0]--;
                                    element.elementBlocks[3][1]++;
                                    element.possition++;
                                } else if (pos == 2) {
                                    element.elementBlocks[0][0] -= 2;
                                    element.elementBlocks[1][0]--;
                                    element.elementBlocks[1][1]--;
                                    element.elementBlocks[3][0]++;
                                    element.elementBlocks[3][1]--;
                                    element.possition = 1;
                                }
                                break;
                            case 4:
                                if (pos == 1) {
                                    element.elementBlocks[0][0] += 2;
                                    element.elementBlocks[0][1] += 2;
                                    element.elementBlocks[1][0]++;
                                    element.elementBlocks[1][1]++;
                                    element.elementBlocks[3][0]--;
                                    element.elementBlocks[3][1]--;
                                    element.possition++;
                                } else if (pos == 2) {
                                    element.elementBlocks[0][0] -= 2;
                                    element.elementBlocks[0][1] -= 2;
                                    element.elementBlocks[1][0]--;
                                    element.elementBlocks[1][1]--;
                                    element.elementBlocks[3][0]++;
                                    element.elementBlocks[3][1]++;
                                    element.possition = 1;
                                }
                                break;
                            case 5:
                                if (pos == 1) {
                                    element.elementBlocks[0][0]++;
                                    element.elementBlocks[0][1]++;
                                    element.elementBlocks[3][0]--;
                                    element.elementBlocks[3][1]++;
                                    element.elementBlocks[1][0]++;
                                    element.elementBlocks[1][1]--;
                                    element.possition++;
                                } else if (pos == 2) {
                                    element.elementBlocks[0][0]--;
                                    element.elementBlocks[0][1]++;
                                    element.elementBlocks[3][0]--;
                                    element.elementBlocks[3][1]--;
                                    element.elementBlocks[1][0]++;
                                    element.elementBlocks[1][1]++;
                                    element.possition++;
                                } else if (pos == 3) {
                                    element.elementBlocks[0][0]--;
                                    element.elementBlocks[0][1]--;
                                    element.elementBlocks[3][0]++;
                                    element.elementBlocks[3][1]--;
                                    element.elementBlocks[1][0]--;
                                    element.elementBlocks[1][1]++;
                                    element.possition++;
                                } else if (pos == 4) {
                                    element.elementBlocks[0][0]++;
                                    element.elementBlocks[0][1]--;
                                    element.elementBlocks[3][0]++;
                                    element.elementBlocks[3][1]++;
                                    element.elementBlocks[1][0]--;
                                    element.elementBlocks[1][1]--;
                                    element.possition = 1;
                                }
                                break;
                            case 6:
                                if (pos == 1) {
                                    element.elementBlocks[2][1] -= 2;
                                    element.elementBlocks[1][0]--;
                                    element.elementBlocks[1][1]++;
                                    element.elementBlocks[3][0]--;
                                    element.elementBlocks[3][1]--;
                                    element.possition++;
                                } else if (pos == 2) {
                                    element.elementBlocks[2][1] += 2;
                                    element.elementBlocks[1][0]++;
                                    element.elementBlocks[1][1]--;
                                    element.elementBlocks[3][0]++;
                                    element.elementBlocks[3][1]++;
                                    element.possition = 1;
                                }
                                break;
                            case 7:
                                if (pos == 1) {
                                    element.elementBlocks[0][0] += 2;
                                    element.elementBlocks[1][0]++;
                                    element.elementBlocks[1][1]--;
                                    element.elementBlocks[3][0]--;
                                    element.elementBlocks[3][1]++;
                                    element.possition++;
                                } else if (pos == 2) {
                                    element.elementBlocks[0][1] += 2;
                                    element.elementBlocks[1][0]++;
                                    element.elementBlocks[1][1]++;
                                    element.elementBlocks[3][0]--;
                                    element.elementBlocks[3][1]--;
                                    element.possition++;
                                } else if (pos == 3) {
                                    element.elementBlocks[0][0] -= 2;
                                    element.elementBlocks[1][0]--;
                                    element.elementBlocks[1][1]++;
                                    element.elementBlocks[3][0]++;
                                    element.elementBlocks[3][1]--;
                                    element.possition++;
                                } else if (pos == 4) {
                                    element.elementBlocks[0][1] -= 2;
                                    element.elementBlocks[1][0]--;
                                    element.elementBlocks[1][1]--;
                                    element.elementBlocks[3][0]++;
                                    element.elementBlocks[3][1]++;
                                    element.possition = 1;
                                }
                                break;

                        }

                        boolean left = false,
                         right = false,
                         bottom = false;

                        for (int i = 0; i < 4; i++) {
                            if (element.elementBlocks[i][0] < 0) {
                                left = true;
                            } else if (element.elementBlocks[i][0] >= room.fieldWidth) {
                                right = true;
                            } else if (element.elementBlocks[i][1] >= room.fieldHeight) {
                                bottom = true;
                            }
                        }

                        for (int i = 0; i < 4; i++) {
                            if (left) {
                                if (element.type == 4) {
                                    element.elementBlocks[i][0] += 2;
                                } else {
                                    element.elementBlocks[i][0]++;
                                }
                            }
                            if (right) {
                                element.elementBlocks[i][0]--;
                            }
                            if (bottom) {
                                element.elementBlocks[i][1] -= 2;
                            }
                        }
                        break;
                    case KeyEvent.VK_R:
                        room.restart();
                        element = new Element();
                        element.isActive = true;
                        life = true;
                        break;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyTyped(KeyEvent e) {
            }

        });

        thread.start();
    }

    public void define() {
        room = new Room();
        element = new Element();
        element.isActive = true;
    }

    public void paintComponent(Graphics g) {

        if (isFirst) {
            define();

            isFirst = false;
        }

        g.setColor(new Color(50, 50, 50));
        g.fillRect(0, 0, getWidth(), getHeight());

        room.draw(g);
        for (int i = 0; i < 4; i++) {
            element.spawnBlock(element.elementBlocks[i][0], element.elementBlocks[i][1]);
            element.draw(g);
        }

        g.setFont(new Font("Curier New", Font.BOLD, 13));
        g.setColor(new Color(255, 255, 255));
        g.drawString("Rows: " + rowsCleaned, 390, 200);
        g.drawString("Level: " + level, 390, 300);

        if (life == false) {
            g.setFont(new Font("Curier New", Font.BOLD, 60));
            g.setColor(new Color(255, 0, 0));
            g.drawString("Game Over", 90, 250);
            g.setFont(new Font("Curier New", Font.BOLD, 20));
            g.drawString("Press \"R\" to restart.", 140, 300);
        }
    }

    public void run() {
        while (true) {
            if (!isFirst) {

                if (life == true) {
                    if (element.isActive == false) {
                        element = new Element();
                        element.isActive = true;
                    }
                    for (int i = 0; i < 4; i++) {
                        element.physic();
                    }
                }
            }

            repaint();

            try {
                Thread.sleep(1);
            } catch (Exception e) {
            }
        }
    }
}
